/*
 * Copyright 2002-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.context.annotation;

import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.context.annotation.*;

/**
 * A single {@code condition} that must be {@linkplain #matches matched} in order
 * for a component to be registered.
 *
 * <p>Conditions are checked immediately before the bean-definition is due to be
 * registered and are free to veto registration based on any criteria that can
 * be determined at that point.
 *
 * <p>Conditions must follow the same restrictions as {@link BeanFactoryPostProcessor}
 * and take care to never interact with bean instances. For more fine-grained control
 * of conditions that interact with {@code @Configuration} beans consider implementing
 * the {@link ConfigurationCondition} interface.
 *
 * @author Phillip Webb
 * @since 4.0
 * @see ConfigurationCondition
 * @see Conditional
 * @see ConditionContext
 */
@FunctionalInterface
public interface Condition {

  /**
   * Determine if the condition matches.
   *
   * @param context the condition context
   * @param metadata the metadata of the {@link org.springframework.core.type.AnnotationMetadata
   *     class} or {@link org.springframework.core.type.MethodMetadata method} being checked
   * @return {@code true} if the condition matches and the component can be registered, or {@code
   *     false} to veto the annotated component's registration
   *
   *     这个注解主要提供了配置文件能动态加载的能力
   *     属于Spring Framework提供的注解，也是Spring Boot条件装配的基石。
   *     {@link Condition#matches(ConditionContext,AnnotatedTypeMetadata)} 主要提供了是否将配置类注册给Spring
   *     ConfigurationCondition，为什么Spring还会额外提供这个接口？这个接口主要提供了阶段属性
   *     那如何理解阶段属性呢？从Spring的角度来理解，Spring对于扫描，注册，解析配置等等处理都是分阶段的，在不同的阶段会处理不同的逻辑操作
   *     那么如果从这个角度来理解，一方面是从Spring的设计原则上，是属于职责明确，边界清晰，另一方面，对于Spring在每个阶段的处理，可以更细粒度的控制
   *     {@link Condition} 都是由 {@link ConditionEvaluator#shouldSkip(AnnotatedTypeMetadata, ConfigurationCondition.ConfigurationPhase)} 来处理的
   *     其中的阶段属性都是由Spring来设置，因为只有Spring会很明确的了解现在正在处理的组件或者配置类处于哪个阶段，比如扫描，注册，配置类解析等等阶段
   */
  boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata);
}
